package com.sgb.goods.view.activity;

import androidx.fragment.app.Fragment;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.google.android.material.tabs.TabLayoutMediator;
import com.sgb.goods.BR;
import com.sgb.goods.R;
import com.sgb.goods.databinding.GoodsActivitySellOrderListBinding;
import com.sgb.goods.utils.Constants;
import com.sgb.goods.utils.base.BaseActivity;
import com.sgb.goods.utils.base.adapter.BasePagerAdapter;
import com.sgb.goods.view.fragment.OrderListFragment;
import com.sgb.goods.viewmodel.SellOrderListViewModel;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 销售订单列表 (迁移)
 * @author cyj
 */
@Route(path = Constants.ARouter.AR_SELL_ORDER_LIST)
public class SellOrderListActivity extends BaseActivity<GoodsActivitySellOrderListBinding, SellOrderListViewModel> {

    private BasePagerAdapter<Fragment> adapter;
    /** 订单流程标题|订单状态|是否审批单(是审批单传1，否则传空字符串) */
    private final String[][] orderArray = {
            {"全部","",""}, {"审批单","","1"}, {"待接单","0",""},
            {"待签约/确认","99",""}, {"履约中","10",""}, {"已完成","7",""}, {"已取消","8",""}
    };

    @Override
    protected int getLayoutResId() {
        return R.layout.goods_activity_sell_order_list;
    }

    @Override
    protected void initViewModel() {
        mViewModel = createViewModel(SellOrderListViewModel.class, BR.viewModel);
    }

    @Override
    protected void init() {
        mDataBinding.head.title.setText("销售订单");
        mViewModel.getApprovalConfig();
    }

    /** 创建fragment */
    protected List<Fragment> getFragments(boolean hasShenPiDan) {
        List<Fragment> fragments = new ArrayList<>();
        for (String[] paramsArray : orderArray) {
            if (!hasShenPiDan && Arrays.equals(paramsArray, orderArray[1])) {
                continue;
            }
            fragments.add(new OrderListFragment("2", paramsArray[0], paramsArray[1], paramsArray[2]));
        }
        return fragments;
    }

    @Override
    protected void initObserve() {
        mViewModel.type.observe(this, type -> {
            List<Fragment> list = getFragments(type == 1);
            mDataBinding.vpView.setAdapter(adapter = new BasePagerAdapter<>(this));
            mDataBinding.vpView.setOffscreenPageLimit(list.size());
            new TabLayoutMediator(mDataBinding.tabGroup, mDataBinding.vpView, (tab, position) ->
                    tab.setText(((OrderListFragment) list.get(position)).orderProcessName)).attach();
            adapter.setDataList(list);
        });
    }

    @Override
    protected void initListener() {
        mDataBinding.head.ivBack.setOnClickListener(v -> finish());
    }
}

